package com.ruoyi.common.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * 描述：Mybatis-Plus 代码生成器
 * @author 杨宸
 * @date 2025-02-11
 */
public class MybatisGenerateUtil {

    /**
     * 代码生成目录
     */
    public static final String GEN_CODE_DIR = "C:\\Users\\cheny\\IdeaProjects\\Easy-Interview\\ruoyi-common\\src\\main\\java\\com\\ruoyi\\common";

    /**
     * Mapper文件生成目录
     */
    public static final String GEN_MAPPER_DIR = "C:\\Users\\cheny\\IdeaProjects\\Easy-Interview\\ruoyi-common\\src\\main\\resources\\mapper";

    /**
     * 包生成目录
     */
    public static final String PARENT_PACKAGE = "gen";

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://localhost:3306/easy_interview?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8",
            "root",
            "83302355yC@");

    /**
     * 执行 run
     */
    public static void main(String[] args)  {
        FastAutoGenerator.create(DATA_SOURCE_CONFIG).globalConfig((scanner, builder) -> builder
                        .author(scanner.apply("请输入作者："))
                        .fileOverride()
                        .enableSwagger()
                        .commentDate("yyyy-MM-dd")
                        .outputDir(GEN_CODE_DIR))
                .packageConfig(builder -> builder.parent(PARENT_PACKAGE)
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, GEN_MAPPER_DIR)))
                .strategyConfig((scanner, builder) -> builder
                        .addInclude(StringListUtil.toList(scanner.apply("请输入表名, 多个表名用逗号分割："), StringListUtil.SEPARATOR_COMMA))
                        .addTablePrefix(scanner.apply("请输入表名前缀："))
                )
                .templateConfig(builder ->
                        builder.controller("/template/controller")
                                .service("/template/service")
                                .serviceImpl("/template/serviceImpl")
                                .mapper("/template/mapper")
                                .entity("/template/entity")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }


}
